package com.example.operation.musicpaster;

import android.app.Instrumentation;
import android.content.Context;
import android.os.Handler;
import android.os.SystemClock;
import android.support.multidex.MultiDex;
import android.support.v4.text.TextUtilsCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.tencent.smtt.sdk.WebView;

import org.w3c.dom.Text;

import java.net.URLEncoder;

public class PasterActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MultiDex.install(this);
        setContentView(R.layout.activity_paster);

        String input = null;
        if (getIntent() != null) {
            input = getIntent().getStringExtra("input");
        }
        findViewById(R.id.lookfor).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                KeyboardUtils.hideKeyboard(v);
                serach(((TextView) findViewById(R.id.inputtext)).getText().toString());
            }
        });
//        ((EditText) findViewById(R.id.inputtext)).setText("小情歌 苏打绿");
        if (!TextUtils.isEmpty(input)) {
            ((EditText) findViewById(R.id.inputtext)).setText(input);
            findViewById(R.id.lookfor).performClick();
        }
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && ((MyWebview) findViewById(R.id.web)).canGoBack()) {
            ((MyWebview) findViewById(R.id.web)).step = 0;
            ((MyWebview) findViewById(R.id.web)).goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void serach(String text) {
        findViewById(R.id.masklayer).setVisibility(View.GONE);
        //https://www.music.163.com/#/search/m/?s=%E8%AE%B8%E7%BE%8E%E9%9D%99%20%E8%BF%B7%E4%B9%B1
//        http://music.163.com/song/media/outer/url?id=306971.mp3
        ((MyWebview) findViewById(R.id.web)).setOnceSearchPlay("true");
        ((MyWebview) findViewById(R.id.web)).step = 1;
        ((MyWebview) findViewById(R.id.web)).setLoadWebFinish(new MyWebview.LoadWebFinish() {

            @Override
            public void loadwebfinish(String url) {
                if (((MyWebview) findViewById(R.id.web)).getStep() == 2) {
                    ((MyWebview) findViewById(R.id.web)).setLoadWebFinish(new MyWebview.LoadWebFinish() {

                        @Override
                        public void loadwebfinish(final String url) {
                            if (((MyWebview) findViewById(R.id.web)).getStep() == 3) {
                                new Thread() {
                                    @Override
                                    public void run() {
                                        super.run();
                                        //酷狗 高音质在线试听
                                        ((MyWebview) findViewById(R.id.web)).requestFocusFromTouch();
                                        if (url.contains("m.kuwo")) {
                                            try {
                                                Thread.sleep(2000);
                                            } catch (InterruptedException e) {
                                                e.printStackTrace();
                                            }
                                            Instrumentation mInst = new Instrumentation();
                                            mInst.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(),
                                                    SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, 960, 1920 - 320, 0));    //x,y 即是事件的坐标
                                            mInst.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(),
                                                    SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, 960, 1920 - 320, 0));
                                        } else if (url.contains("taihe")) {
                                            try {
                                                Thread.sleep(2000);
                                            } catch (InterruptedException e) {
                                                e.printStackTrace();
                                            }
                                            Instrumentation mInst = new Instrumentation();
                                            mInst.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(),
                                                    SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, 540, 1920 - 190, 0));    //x,y 即是事件的坐标
                                            mInst.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(),
                                                    SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, 540, 1920 - 190, 0));
                                        } else if (url.contains("music.163")) {
                                            try {
                                                Thread.sleep(3000);
                                            } catch (InterruptedException e) {
                                                e.printStackTrace();
                                            }
                                            Instrumentation mInst = new Instrumentation();
                                            mInst.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(),
                                                    SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, 960, 540, 0));    //x,y 即是事件的坐标
                                            mInst.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(),
                                                    SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, 540, 960, 0));
                                        }

                                    }
                                }.start();

                            }
                        }
                    });
                    new Handler().post(new Runnable() {
                        @Override
                        public void run() {
                            ((WebView) findViewById(R.id.web)).loadUrl(((MyWebview) findViewById(R.id.web)).getOnly1());

                        }
                    });
                }

            }
        });
//        http://music.taihe.com/search?fr=ps&ie=utf-8&key=%E8%AE%B8%E7%BE%8E%E9%9D%99+%E8%BF%B7%E4%B9%B1
        ((WebView) findViewById(R.id.web)).loadUrl("https://m.baidu.com/s?ie=utf-8&f=8&rsv_bp=0&rsv_idx=1&tn=baidu&wd=" + URLEncoder.encode("" + text) + "&rsv_pq=d4f7b35b0000856d&rsv_t=4225iLLyyekqQo44BcY%2BDmxj8J05QtwWucqxZF8T22YZVdmqAfcjzH%2F8M%2FU&rqlang=cn&rsv_enter=1&rsv_sug3=7&rsv_sug1=6&rsv_sug7=101");
//        ((WebView)findViewById(R.id.web)).loadUrl("javascript:{list result.order as index}\n" +
//                "{var lst=result[index]}\n" +
//                "{if !!lst&&!!lst.length}\n" +
//                "{if index==\"songs\"}\n" +
//                "{list lst as song}\n" +
//                "alert(${song.id+''+song.name})"+
//                "{/list}\n" +
//                "</ul>\n" +
//                "{elseif index==\"artists\"}\n" +
//                "{list lst as artist}\n" +
//                "{/list}");

    }

    public static class KeyboardUtils {

        public static void showKeyboard(View view) {
            InputMethodManager imm = (InputMethodManager) view.getContext()
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm != null) {
                view.requestFocus();
                imm.showSoftInput(view, 0);
            }
        }

        public static void hideKeyboard(View view) {
            InputMethodManager imm = (InputMethodManager) view.getContext()
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm != null) {
                imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
            }
        }

        public static void toggleSoftInput(View view) {
            InputMethodManager imm = (InputMethodManager) view.getContext()
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm != null) {
                imm.toggleSoftInput(0, 0);
            }
        }
    }
}
